package com.smart.upms.sys.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.smart.core.starter.constant.CommonConstant;
import com.smart.core.starter.constant.MqQueueConstant;
import com.smart.core.starter.utils.CopyUtils;
import com.smart.upms.sys.entity.SysZuulRoute;
import com.smart.upms.sys.mapper.SysZuulRouteMapper;
import com.smart.upms.sys.param.SysZuulRouteParam;
import com.smart.upms.sys.param.SysZuulRouteQueryParam;
import com.smart.upms.sys.result.SysZuulRouteResult;
import com.smart.upms.sys.service.ISysZuulRouteService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 动态路由配置 服务实现类
 * </p>
 *
 * @author guxiaobai
 * @since 2018-12-11
 */
@Service
@AllArgsConstructor
@Slf4j
public class SysZuulRouteServiceImpl implements ISysZuulRouteService {

    private final SysZuulRouteMapper mapper;
    private final RabbitTemplate rabbitTemplate;
    private final RedisTemplate<String, String> redisTemplate;


    @Override
    public Boolean save(SysZuulRouteParam param) {
        SysZuulRoute entity = CopyUtils.copyObject(param, SysZuulRoute.class);
        int insert = mapper.insert(entity);
        return insert > 0;
    }

    @Override
    public Boolean update(SysZuulRouteParam param) {
        SysZuulRoute entity = CopyUtils.copyObject(param, SysZuulRoute.class);
        int update = mapper.updateById(entity);
        return update > 0;

    }

    @Override
    public Boolean delete(Long id) {
        int delete = mapper.deleteById(id);
        return delete > 0;
    }

    @Override
    public SysZuulRouteResult get(Long id) {
        SysZuulRoute entity = mapper.selectById(id);
        return CopyUtils.copyObject(entity, SysZuulRouteResult.class);
    }

    @Override
    public List<SysZuulRouteResult> list(SysZuulRouteQueryParam param) {
        QueryWrapper<SysZuulRoute> queryWrapper = new QueryWrapper<>();
        List<SysZuulRoute> entityList = mapper.selectList(queryWrapper);
        return CopyUtils.copyList(entityList, SysZuulRouteResult.class);
    }

    @Override
    public Page<SysZuulRouteResult> listPage(Page<SysZuulRouteResult> page, SysZuulRouteQueryParam param) {
        QueryWrapper<SysZuulRoute> queryWrapper = new QueryWrapper<>();
        Page<SysZuulRoute> entityPage = new Page<>();
        entityPage.setSize(page.getSize());
        entityPage.setCurrent(page.getCurrent());
        entityPage.setAsc(page.ascs());
        entityPage.setDesc(page.descs());
        mapper.selectPage(entityPage, queryWrapper);
        Page<SysZuulRouteResult> resultPage = CopyUtils.copyPage(entityPage, SysZuulRouteResult.class);
        return resultPage;
    }

    @Override
    public Boolean apply() {
        QueryWrapper<SysZuulRoute> queryWrapper = new QueryWrapper<>();
        List<SysZuulRoute> entityList = mapper.selectList(queryWrapper);
        List<SysZuulRouteResult> routeResults = CopyUtils.copyList(entityList, SysZuulRouteResult.class);
        redisTemplate.opsForValue().set(CommonConstant.ROUTE_KEY, JSON.toJSONString(routeResults));
        //发送消息，刷新路由
        rabbitTemplate.convertAndSend(MqQueueConstant.ROUTE_CONFIG_CHANGE, true);
        log.debug("刷新路由信息：{}", routeResults);
        return true;
    }


}
